export const callout = {
  data() {
    return {
      isHandle: false, // 判断是否是手动拨出
      flag: 0,
      mxTimer:null
    }
  },
  computed: {
    mxoutWsState() {
      return websocket.state
    },
    mxoutWsData() {
      return websocket.data
    },
    mxoutUserInfo() {
      return this.$store.getters.getUserInfo
    },
  },
  watch: {
    mxoutWsData(val) {
      const isHandle = this.isHandle
      if (isHandle && val.action === 'pickup') {
        this.pickupHandler(val)
      }

      if (isHandle && val.cmd === 'callout' && websocket.pickup && !websocket.callin) {
        this.calloutRetHandler(val)
      }
    }
  },
  methods: {
    //只有回访才是1
    callout(val, flag = 0) {
      if (!this.isHandle) {
        this.flag = flag
        if (new RegExp('^0*((13[0-9])|(15[^,\\D])|(17[0-9])|(147)|(18[0-9])|(19[0-9]))\\d{8}$').test(val) === false) {
          return this.$warn('请输入正确的手机号码')
        }
        // 别名
        // const ws = websocket
        const wsstate = this.mxoutWsState
        const cmd = 'pickup'
        if (wsstate === 1) {
          this.isHandle = true
          websocket.outPhone = val
          // 发送摘机指令
          websocket.WsSend(cmd)
          this.$vux.loading.show({
            text: 'Loading'
          })
          return
        }
        this.$toast({
          message: '连接尚未建立'
        })
      } else {
        this.$toast({
          message: '正在通话中，请勿频繁拨打'
        })
      }
    },
    pickupHandler(val) {
      if (val.result === '0') {
        websocket.pickup = true
        websocket.WsSend('out') // 摘机成功立马呼出

        // 添加超时处理
        this.mxTimer = setTimeout(()=>{
          // 信息初始化
          websocket.pickup = false
          websocket.outPhone = ''
          this.isHandle = false
          // 关闭loading
          this.$vux.loading.hide()
          this.$toast({
            message:'电话超时'
          })
        },15 * 1000)
      } else {
        this.$toast({
          message: '未摘机，请重新输入呼叫号码'
        })
      }
    },
    calloutRetHandler(val) {
      // 新的方式
      const {
        action,
        result
      } = val

      // 终止超时处理
      if(this.mxTimer){
        clearTimeout(this.mxTimer)
      }
      if (action === 'out' && result === '2') {
        websocket.pickup = false
        // 如果出现Loading就关闭
        this.$vux.loading.hide()
      } else if (action === 'call' && this.flag && this.tableData) {
        this.updateResult()
      }
      this.isHandle = false
      return
    },
  }
}

export const utils = {
  methods: {
    _dateFormat(date, fmt) {
      const o = {
        "M+": date.getMonth() + 1, //月份
        "d+": date.getDate(), //日
        "h+": date.getHours(), //小时
        "m+": date.getMinutes(), //分
        "s+": date.getSeconds(), //秒
        "q+": Math.floor((date.getMonth() + 3) / 3), //季度
        "S": date.getMilliseconds() //毫秒
      }
      if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
      for (let k in o) {
        if (new RegExp("(" + k + ")").test(fmt)) {
          fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
        }
      }
      return fmt;
    },
    _isEmptyObject(o) {
      return Object.prototype.toString.call(o) === '[object Object]' && Object.keys(o).length === 0 ? true : false
    },
  }
}
